// pages/my/my.js
let user_s = "";
var make = {};
// var host = "http://localhost:8085/";
// var host = "http://localhost:8085/";
var call = require("../../utils/ajax.js")
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        openid: [],
        hideModal: true,
        kk: true,
        M: {},
        bangding: false,
        sate: {},
        user_name: "Muskmelon",
        user_state: "未认证协会",
        userLogin: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */

    onGin: function(FUC) {
        var admin = this;
        wx.login({
            //获取code
            success: function(res) {
                var code = res.code; //返回code
                console.log(res);
                let url = "login/auth?code=" + code;
                call.request2(url, "", function(res) {
                    console.log(res)
                    if (res.message == "成功获取code") {
                        var openid = res.date.openid //返回openid
                        console.log('openid为' + openid);
                        admin.setData({
                            openid: openid
                        })
                        user_s = openid
                        setTimeout(FUC, 1000); //一秒时间后执行
                    } else {
                        wx.showToast({
                            title: '绑定失败，请重新绑定。',
                            icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                            duration: 2000
                        })
                        admin.setData({
                            bangding: false
                        })
                    }
                }, function(res) {
                    wx.showToast({
                        title: '绑定失败，请重新绑定。',
                        icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                        duration: 2000
                    })
                    admin.setData({
                        bangding: false
                    })
                })
            }
        })
    },
    show: function() {
        var that = this;
        that.setData({
            hideModal: false
        })
        var animation = wx.createAnimation({
            duration: 100, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
            timingFunction: 'ease', //动画的效果 默认值是linear
        })
        this.animation = animation
        setTimeout(function() {
            that.fadeIn(); //调用显示动画
        }, 100)
    },
    hide: function() {
        var that = this;
        var animation = wx.createAnimation({
            duration: 100, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
            timingFunction: 'ease', //动画的效果 默认值是linear
        })
        this.animation = animation
        that.fadeDown(); //调用隐藏动画   
        setTimeout(function() {
                that.setData({
                    hideModal: true
                })
            }, 100) //先执行下滑动画，再隐藏模块
    },
    getM: function() {
        wx.getUserInfo({
            withCredentials: true,
            lang: '',
            success: function(res) {
                console.log(res)
            },
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    erroHide: function() {
        var that = this;
        var animation = wx.createAnimation({
            duration: 100, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
            timingFunction: 'ease', //动画的效果 默认值是linear
        })
        this.animation = animation
        that.fadeDown(); //调用隐藏动画   
        setTimeout(function() {
                that.setData({
                    kk: true
                })
            }, 100) //先执行下滑动画，再隐藏模块
    },
    erroShow: function() {
        var that = this;
        that.setData({
            kk: false
        })
        var animation = wx.createAnimation({
            duration: 100, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
            timingFunction: 'ease', //动画的效果 默认值是linear
        })
        this.animation = animation
        setTimeout(function() {
            that.fadeIn(); //调用显示动画
        }, 100)
    },
    sendUser: function(user) {
        var kk = this;
        var that = this;
        let openId = user_s;
        let data = {};
        console.log("id为：" + openId)
        try {
            data = {
                user_nick_name: user.nickName,
                user_id: openId,
                user_phone: "string",
                user_picture: user.avatarUrl
            }
        } catch (e) {
            console.log(e)
        }

        let url = "login/signUp";
        call.request3(url, data, function(res) {
            console.log(res);
            if (res.data.message == "登陆成功") {
                let uu = res.data.date.user;
                let make = "";
                let userState = res.data.date.organization;
                // let userState = false;
                make = res.header["x-auth-token"];
                wx.setStorageSync('mm', uu);
                //存储token
                wx.setStorageSync('tokenID', make);
                let make1 = "";
                make1 = wx.getStorageSync('tokenID');
                //设置登录状态
                app.globalData.userState = true;
                console.log(make1)
                    //存储用户是否为协会用户
                wx.setStorageSync('userState', userState);

                let admisn = wx.getStorageSync('userState');
                console.log(admisn)
                kk.setData({
                    M: res.data.date.user
                })
                console.log("登录了")
                    //存储用户是否有机构,判断是否为true
                let userLogin = false;
                //存储openID
                console.log(openId)
                wx.setStorageSync('openID', openId);
                userLogin = res.data.date.user.user_status;
                if (userLogin) {
                    console.log("ss")
                    that.setData({
                        user_state: userState.organization_name
                    })
                    app.globalData.userLogin = userLogin;
                }
            } else {
                wx.showToast({
                    title: '登录失败！',
                    icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                    duration: 2000
                })
                that.setData({
                    bangding: false
                })
                console.log("未登录")
            }

        }, function(e) {

            console.log(e)
        })

    },
    //此处用做识别是否登录，登陆了就返回数据，可以获取用户在数据库的id
    lal: function() {
        let kk = this;
        console.log("s")
        make = wx.getStorageSync('mm')
        return make;
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onLoad: function(options) {
        let that = this;
        let admin = this;
        let state = admin.lal();
        console.log(state)
        if (state.user_id != undefined || state.user_id != null) {
            admin.setData({
                bangding: true,
                M: state
            });
        }
    },
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    kk: function() {

    },
    // 显示遮罩层
    showModal: function() {
        this.show();
    },
    // 隐藏遮罩层
    onBin: function(e) {
        let jj = this;
        let user = e.detail.userInfo;
        this.hide();

        console.log(e.detail.userInfo);
        if (e.detail.userInfo != undefined && e.detail.userInfo != null) {
            jj.setData({
                bangding: true
            })
            const promise = new Promise(function(resolve, reject) {
                jj.onGin(resolve);
            })
            promise.then(function() {
                jj.sendUser(user);
            }, function() {
                wx.showToast({
                    title: '绑定失败，请重新绑定。',
                    icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                    duration: 2000
                })
                jj.setData({
                    bangding: false
                })
            })

        } else {
            wx.showToast({
                title: '绑定失败，请检查网络。',
                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                duration: 2000
            })
            jj.setData({
                    bangding: false
                })
                // jj.erroShow();
                // setTimeout(function() {
                //     jj.erroHide(); //调用显示动画
                // }, 2000)
        }
    },

    ofFBin: function() {
        this.hide();
    },

    //动画集
    fadeIn: function() {
        this.animation.translateY(0).step()
        this.setData({
            animationData: this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性
        })
    },
    fadeDown: function() {
        this.animation.translateY(300).step()
        this.setData({
            animationData: this.animation.export(),
        })
    },


})